<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="终端编号" prop="clientNo">
        <el-input
          v-model="queryParams.clientNo"
          placeholder="请输入终端编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="终端名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入终端名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属部门" prop="orgId">
        <treeselect v-model="queryParams.orgId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" style="max-width: 240px"/>
      </el-form-item>
      <el-form-item label="最后通讯">
        <el-date-picker
          v-model="daterangeLastTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="设备状态" prop="version">
        <el-select
          v-model="queryParams.clientStat"
          placeholder="设备状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.quake_device_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所在地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入所在地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['device:quakeClient:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['device:quakeClient:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['device:quakeClient:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="quakeClientList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column sortable label="数据编号" align="center" prop="id" />
      <el-table-column sortable label="终端编号" align="center" prop="clientNo" />
      <el-table-column sortable label="终端名称" align="center" prop="clientName" />
      <el-table-column label="归属单位" align="center" prop="deptName" />
      <el-table-column sortable label="注册时间" align="center" prop="addTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="最后通讯" align="center" prop="lastTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="在线状态" align="center" prop="clientStat">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.quake_device_status" :value="scope.row.clientStat"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="设备类型" align="center" prop="clientType" />-->
<!--      <el-table-column label="版本号" align="center" prop="version" />-->
      <el-table-column label="备注" align="center" prop="remark" />
<!--      <el-table-column label="在线时长" align="center" prop="onlineTimes" />-->
<!--      <el-table-column label="经度" align="center" prop="longitude" />-->
<!--      <el-table-column label="纬度" align="center" prop="latitude" />-->
      <el-table-column label="所在地址" align="center" prop="address" />
      <el-table-column label="管理人" align="center" prop="nickName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--<router-link :to="'/gasClient/realtimeWaveform/' + scope.row.clientNo" target="_blank" class="link-type">
            <span>实时波形</span>
          </router-link>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-monitor"
            @click="realtimeWaveform(scope.row)"
            v-hasPermi="['device:quakeClient:edit']"
          >实时波形</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['device:quakeClient:edit']"
          >修改</el-button>
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['device:quakeClient:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改震动监测设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="终端名称" prop="clientName">
          <el-input v-model="form.clientName" placeholder="请输入终端名称" />
        </el-form-item>
        <el-form-item label="机构ID" prop="orgId">
          <el-input v-model="form.orgId" placeholder="请输入机构ID" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
<!--        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>-->
        <el-form-item label="所在地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入所在地址" />
        </el-form-item>
        <el-form-item label="管理人id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入管理人id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQuakeClient, getQuakeClient, delQuakeClient, addQuakeClient, updateQuakeClient } from "@/api/device/quakeClient";
import {treeselect} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "QuakeClient",
  dicts: ['quake_device_status'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 震动监测设备表格数据
      quakeClientList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 管理人id时间范围
      daterangeAddTime: [],
      // 管理人id时间范围
      daterangeLastTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientNo: null,
        clientName: null,
        orgId: null,
        addTime: null,
        lastTime: null,
        clientType: null,
        clientStat: null,
        address: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        clientNo: [
          { required: true, message: "终端编号不能为空", trigger: "blur" }
        ],
        lastTime: [
          { required: true, message: "最后通讯不能为空", trigger: "blur" }
        ],
      },
      // 部门树选项
      deptOptions: undefined,
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询震动监测设备列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeAddTime && '' != this.daterangeAddTime) {
        this.queryParams.params["beginAddTime"] = this.daterangeAddTime[0];
        this.queryParams.params["endAddTime"] = this.daterangeAddTime[1];
      }
      if (null != this.daterangeLastTime && '' != this.daterangeLastTime) {
        this.queryParams.params["beginLastTime"] = this.daterangeLastTime[0];
        this.queryParams.params["endLastTime"] = this.daterangeLastTime[1];
      }
      listQuakeClient(this.queryParams).then(response => {
        this.quakeClientList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        clientNo: null,
        clientName: null,
        orgId: null,
        addTime: null,
        lastTime: null,
        clientType: null,
        version: null,
        remark: null,
        onlineTimes: null,
        longitude: null,
        latitude: null,
        address: null,
        userId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeAddTime = [];
      this.daterangeLastTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加震动监测设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getQuakeClient(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改震动监测设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateQuakeClient(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuakeClient(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除震动监测设备编号为"' + ids + '"的数据项？').then(function() {
        return delQuakeClient(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('device/quakeClient/export', {
        ...this.queryParams
      }, `quakeClient_${new Date().getTime()}.xlsx`)
    },
    realtimeWaveform(row){
      this.$router.push({ path: '/gasClient/realtimeWaveform/' + row.clientNo});
    }
  }
};
</script>
